<?php

class SystemAction extends Action
{
	public $key = array(
		'1' => array('key'=>1,'name'=>'系统管理'),
		'2' => array('key'=>2,'name'=>'支付配送管理'),
		'3' => array('key'=>3,'name'=>'商家管理'),
		'4' => array('key'=>4,'name'=>'商品管理'),
		'5' => array('key'=>5,'name'=>'礼品卡管理'),
		'6' => array('key'=>6,'name'=>'优惠券管理'),
		'7' => array('key'=>7,'name'=>'订单管理'),
		'8' => array('key'=>8,'name'=>'文章管理'),
		'9' => array('key'=>9,'name'=>'用户管理'),
		'10' => array('key'=>10,'name'=>'广告管理'),
		);
  
 	public function preMethod()
	{
		R("Mayijie/check_admin");
		$perm_key = session('admin_perm');
		$perm_arr = explode(",",$perm_key);
		if(!in_array(1,$perm_arr)){
			$this->error("无权操作");
		}
	}

    //基本信息
	public function systems(){
		$file=file_get_contents('core/Conf/config.php');
		preg_match_all("/'WEB_NAME'.*?=>.*?'(.*?)'/",$file,$web_name);
		$this->web_name=$web_name[1][0];
		preg_match_all("/'LIST_PAGE_SIZE'.*?=>(.*?),/",$file,$list_size);
		$this->list_size=$list_size[1][0];
		preg_match_all("/'COMMENT_PAGE_SIZE'.*?=>(.*?),/",$file,$comment_size);
		$this->comment_size=$comment_size[1][0];
		preg_match_all("/'CART_COOKIE'.*?=>.*?'(.*?)'/",$file,$cookie_time);
		$this->cookie_time=$cookie_time[1][0];
		preg_match_all("/'ICP_CODE'.*?=>.*?'(.*?)'/",$file,$icp);
		$this->icp=$icp[1][0];
		preg_match_all("/'KEFU_QQ'.*?=>.*?'(.*?)'/",$file,$qq);
		$this->qq=$qq[1][0];
		preg_match_all("/'KEFU_TEL'.*?=>.*?'(.*?)'/",$file,$tel);
		$this->tel=$tel[1][0];
		$this->display();
	}
    //保存基本信息
	public function systems_save(){
		$file=file_get_contents('core/Conf/config.php');
		$name=$this->_post('name');
		$list_size=$this->_post('list_size');
		$comment_size=$this->_post('comment_size');
		$cookie_time=$this->_post('cookie_time');
		$icp=$this->_post('icp');
		$qq=$this->_post('qq');
		$tel=$this->_post('tel');

		$file=preg_replace("/'WEB_NAME'.*?=>.*?'(.*?)'/","'WEB_NAME'=>'{$name}'",$file);
		$file=preg_replace("/'LIST_PAGE_SIZE'.*?=>(.*?),/","'LIST_PAGE_SIZE'=>{$list_size},",$file);
		$file=preg_replace("/'COMMENT_PAGE_SIZE'.*?=>(.*?),/","'COMMENT_PAGE_SIZE'=>{$comment_size},",$file);
		$file=preg_replace("/'CART_COOKIE'.*?=>.*?'(.*?)'/","'CART_COOKIE'=>'{$cookie_time}'",$file);
		$file=preg_replace("/'ICP_CODE'.*?=>.*?'(.*?)'/","'ICP_CODE'=>'{$icp}'",$file);
		$file=preg_replace("/'KEFU_QQ'.*?=>.*?'(.*?)'/","'KEFU_QQ'=>'{$qq}'",$file);
		$file=preg_replace("/'KEFU_TEL'.*?=>.*?'(.*?)'/","'KEFU_TEL'=>'{$tel}'",$file);
		file_put_contents('core/Conf/config.php',$file);
		echo 1;
	}

	//邮件配置
	public function email(){
		$action=$this->_get('action');
		$file=file_get_contents('core/Conf/config.php');
		if($action=='save'){
			$reg=$this->_post('reg');
			$order=$this->_post('order');
			$pay=$this->_post('pay');
			$ship=$this->_post('ship');
			$file=preg_replace("/'EMAIL_REG'.*?=>.*?'(.*?)'/","'EMAIL_REG'=>'{$reg}'",$file);
			$file=preg_replace("/'EMAIL_CREATE_ORDER'.*?=>.*?'(.*?)'/","'EMAIL_CREATE_ORDER'=>'{$order}'",$file);
			$file=preg_replace("/'EMAIL_PAY'.*?=>.*?'(.*?)'/","'EMAIL_PAY'=>'{$pay}'",$file);
			$file=preg_replace("/'EMAIL_SHIP'.*?=>.*?'(.*?)'/","'EMAIL_SHIP'=>'{$ship}'",$file);
			file_put_contents('core/Conf/config.php',$file);
		    echo 1;
		}else{
			preg_match_all("/'EMAIL_REG'.*?=>.*?'(.*?)'/",$file,$reg);
			$this->reg=$reg[1][0];
			preg_match_all("/'EMAIL_CREATE_ORDER'.*?=>.*?'(.*?)'/",$file,$order);
			$this->order=$order[1][0];
			preg_match_all("/'EMAIL_PAY'.*?=>.*?'(.*?)'/",$file,$pay);
			$this->pay=$pay[1][0];
			preg_match_all("/'EMAIL_SHIP'.*?=>.*?'(.*?)'/",$file,$ship);
			$this->ship=$ship[1][0];
			$this->display();
		}
	}

	public function email_sys(){
		$file=file_get_contents('core/Conf/config.php');
		preg_match_all("/'SMTP_HOST'.*?=>.*?'(.*?)'/",$file,$smtp_name);
		$this->smtp_name=$smtp_name[1][0];
		preg_match_all("/'SMTP_PORT'.*?=>.*?'(.*?)'/",$file,$smtp_port);
		$this->smtp_port=$smtp_port[1][0];
		preg_match_all("/'SMTP_USER'.*?=>.*?'(.*?)'/",$file,$user);
		$this->user=$user[1][0];
		preg_match_all("/'SMTP_PASS'.*?=>.*?'(.*?)'/",$file,$pwd);
		$this->pwd=$pwd[1][0];
		preg_match_all("/'FROM_EMAIL'.*?=>.*?'(.*?)'/",$file,$from_email);
		$this->from_email=$from_email[1][0];
		preg_match_all("/'FROM_NAME'.*?=>.*?'(.*?)'/",$file,$from_name);
		$this->from_name=$from_name[1][0];
		$this->display();
	}

	public function email_sys_save(){
		$file=file_get_contents('core/Conf/config.php');
		$smtp_name=$this->_post('smtp_name');
		$smtp_port=$this->_post('smtp_port');
		$user=$this->_post('user');
		$from_email=$this->_post('from_email');
		$pwd=$this->_post('pwd');
		$from_name=$this->_post('from_name');
		$file=preg_replace("/'SMTP_HOST'.*?=>.*?'(.*?)'/","'SMTP_HOST'=>'{$smtp_name}'",$file);
		$file=preg_replace("/'SMTP_PORT'.*?=>.*?'(.*?)'/","'SMTP_PORT'=>'{$smtp_port}'",$file);
		$file=preg_replace("/'SMTP_USER'.*?=>.*?'(.*?)'/","'SMTP_USER'=>'{$user}'",$file);
		$file=preg_replace("/'SMTP_PASS'.*?=>.*?'(.*?)'/","'SMTP_PASS'=>'{$pwd}'",$file);
		$file=preg_replace("/'FROM_EMAIL'.*?=>.*?'(.*?)'/","'FROM_EMAIL'=>'{$from_email}'",$file);
		$file=preg_replace("/'FROM_NAME'.*?=>.*?'(.*?)'/","'FROM_NAME'=>'{$from_name}'",$file);
		file_put_contents('core/Conf/config.php',$file);
		echo 1;
	}

	//邮件内容模板
	public function email_tpl(){
		$Tpl=M('Email_tpl');
		$map['type']=$this->_get('type');
		$action=$this->_get('action');
		if($action=='save'){
			$data['tpl']=stripslashes(htmlspecialchars_decode($_POST['tpl']));
			$map2['type']=$this->_post('type');
			$Tpl->where($map2)->save($data);
			echo 1;
		}else{
			$tpl=$Tpl->where($map)->find();
			if(false==$tpl){
				$Tpl->add($map);
				$tpl=$Tpl->where($map)->find();
			}
			$this->tpl=$tpl;
			$this->display();
		}
	}

	public function sms(){
		$this->display();
	}

	public function admin()
	{
		$key = $this->key;
		$list = AdminModel::getAdminList();
		foreach($list as &$info){
			switch($info['level']){
				case 1:$role = '系统管理员';break;
				case 2:$role = '商品管理员';break;
				case 3:$role = '财务管理员';break;
				case 4:$role = '订单管理员';break;
				case 5:$role = '运营管理员';break;
				case 6:$role = '用户管理员';break;
				case 99:$role = "超级管理员";break;
			}
			$info['role'] = $role;

			$perm = explode(',',$info['perm_key']);
			$arr = array();
			foreach($perm as $v){
				$arr[] = "【".$key[$v]['name']."】";
			}
			$info['perm'] = implode("   ",$arr);
		}
		$this->assign("list",$list);
		$this->display();
	}

	public function addAdmin()
	{
		$this->display();
	}

	public function doAdd()
	{
		$map['uname'] = $this->_post("admin");
		$res = AdminModel::getAdminInfo($map);
		if(!empty($res)){
			$this->error("此用户已经存在");
		}
		$data['uname'] = $map['uname'];
		$data['password'] = md5($this->_post('password'));
		$data['create_time'] = time();
		$data['level'] = $this->_post('level');
		$perm_key = $this->_post('perm');
		$data['perm_key'] = implode(",",$perm_key);
		$re = AdminModel::addAdmin($data);
		if(!empty($re)){
			$this->success("添加成功!");
		}else{
			$this->error("添加失败!");
		}
	}

	public function updateAdmin()
	{
		$options['id'] = $this->_get('id');
		$admin = AdminModel::getAdminInfo($options);
		$my_perm = explode(',',$admin['perm_key']);
		$key = $this->key;
		foreach($key as $k=>&$v){
			if(in_array($k,$my_perm)){
				$v['have'] = 0;
			}else{
				$v['have'] = 1;
			}
		}

		$this->assign("admin",$admin);
		$this->assign("perm",$key);
		$this->display();
	}

	public function doupAdmin()
	{
		$map['id'] = $this->_post('id');
		$pass = $this->_post('password');
		if(!empty($pass)){
			$data['password'] = md5($pass);
		}
		$data['level'] = $this->_post('level');
		$perm_key = $this->_post('perm');
		$data['perm_key'] = implode(",",$perm_key);

		$res = AdminModel::updateAdmin($map, $data);
		if(!empty($res)){
			$this->success("修改成功");
		}else{
			$this->error("修改失败");
		}
	}

	public function delAdmin()
	{
		$id = $this->_get('id');
		AdminModel::delAdmin($id);
	}


}
?>